import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    count: 10,
    number: 100,
    name: 'YK',
    age: 24,
    todos: [
      {id: 1, text: '...', done: true},
      {id: 2, text: '...', done: false}
    ],
    TestArr: [1,2,5],
    TestObj:{
        name:'YK'
    }
  },
  getters: {
    doneTodos: state => {
      //下面的fillter是数组过滤方法，参数是回调函数，回调函数的参数是数组的每一项
      return state.todos.filter(todo => todo.done)
    },
    Double_count: (state, getters) => {
      return state.count * 2 + '----' + getters.doneTodos[0].id;
    },
    TestArr_getters: state => {
      return state.TestArr
    }
  },
  mutations: {
    add(state) {
      state.count++;
    },
    reduce(state, obj) {
      state.count = state.count - obj.amount;
    },
    changeTestArr(state, obj) {
      Vue.set(state.TestArr,2,3) //state.TestArr[2]=3
      Vue.set(state.TestObj,'age',11) // state.TestObj.age=11
    }

  },
  actions: {
    async_add(context) {
      setTimeout(function () {
        context.commit('add')
        alert('已加1')
      }, 1000)
    },
    changeTestArr_action(context){
      setTimeout(function () {
        console.log(123)
        context.commit('changeTestArr',{new:7})
      }, 1000)
    }

  }
});
